<!--
  * 信息变更 弹窗
  * 
-->
<template>
  <a-modal v-model:visible="visible" :title="changeState?'变更状态':'审批任务'" destroyOnClose>
    <DictSelectType ref="dictSelectType" :service-id="SERVICE_ID_ENUM.DICTIONARY.value" :dcit-type="changeState?DICT_TYPE_ENUM.STATE.value:DICT_TYPE_ENUM.APPROVE.value"  :height="400" v-model:value="selectValue" />
    <div class="footer">
      <a-button style="margin-right: 8px" @click="closeModal">取消</a-button>
      <a-button type="primary" @click="handleOk">提交</a-button>
    </div>
  </a-modal>
</template>
<script setup lang="ts">
  import { message } from 'ant-design-vue';
import _ from 'lodash';
import { ref } from 'vue';
import DictSelectType from '/@/components/cache/dict-select-type.vue'
import { taskApi } from '/@/api/task/task-api';
import { SERVICE_ID_ENUM,DICT_TYPE_ENUM } from '/@/constants/system/cache-service-id-const';
import { smartSentry } from '/@/lib/smart-sentry';
import { SmartLoading } from '/@/components/framework/smart-loading';

  // ----------------------- 以下是字段定义 emits props ---------------------

  const emit = defineEmits(['refresh']);

  // ----------------------- 显示/隐藏 ------------------------

  const dictSelectType = ref();
  const visible = ref(false);
  const taskIdList = ref([]);
  const changeState=ref(false);
  const selectValue =ref();

  //显示
  async function showModal(selectTaskId,isState) {
    taskIdList.value = selectTaskId;
    changeState.value=isState;
    visible.value = true;
  }

  //隐藏
  function closeModal() {
    visible.value = false;
  }

  // ----------------------- form操作 ---------------------------------
  async function handleOk() {
    SmartLoading.show();
    try {
      if (_.isEmpty(taskIdList.value)) {
        message.warning('请选择要变更的任务');
        return;
      }
      if (_.isEmpty(selectValue)) {
        message.warning('请选择要变更的信息');
        return;
      }
      let updateValue = selectValue.value;
      let params = {
        taskIdList: taskIdList.value,
        updateValue: updateValue,
        changeState: changeState.value
      };
      let res=await taskApi.batchUpdateTask(params);
      if(res.success){
        message.success('操作成功');
        emit('refresh');
        closeModal();
      }
      else
        message.error(res.message);
      
    } catch (error) {
      smartSentry.captureError(error);
    } finally {
      SmartLoading.hide();
    }
  }

  // ----------------------- 以下是暴露的方法内容 ----------------------------
  defineExpose({
    showModal,
  });
</script>
<style scoped lang="less">
  .footer {
    position: absolute;
    right: 0;
    bottom: 0;
    width: 100%;
    border-top: 1px solid #e9e9e9;
    padding: 10px 16px;
    background: #fff;
    text-align: right;
    z-index: 1;
  }
</style>
